/* $Id:action_bonus.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "action_bonus"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "action_bonus.h"
#include "../general/general.h"

action_bonus::action_bonus() {
    nr=0;
    points=0;
    type=-1;
}

// Field : nr

const int action_bonus::getNr() const { return nr; }
void action_bonus::setNr(const int v) { nr=v; }
int action_bonus::operator<(const class action_bonus &ck) const { return (this->nr < ck.nr)?1:0; }

// Field : points

const int action_bonus::getPoints() const { return points; }
void action_bonus::setPoints(const int v) { points=v; }

// Field : type

const string action_bonusType[]={"less_energy","less_endurance","less_space_needed","less_time","extra_attack","extra_defense","extra_effect","double_range","group_size","extra_age","extra_meter","less_cutting","less_thrusting","less_blunt","less_damage","extra_item","extra_person","extra_day","permanency"};
const int action_bonus::getType() const { return type; }
const string action_bonus::showType() const {
    if (type<0) return "";
    else return action_bonusType[type];
}
void action_bonus::setType(const int  v) {
    if (v<0 || (unsigned int)v >= sizeof action_bonusType/sizeof action_bonusType[0]) type=-1;
    else type=v;
}
void action_bonus::setType(const string v) {
    type=-1;
    loopArray(action_bonusType, tp, string) {
        if (v==*tp) {type=tp_i; break; }
    }
    if (type==-1) dbg_state("Enumerate action_bonus.type unknown '%s'", v.c_str());
}

void action_bonus::save(class channel &ch) {
    ch.write("action_bonus", "");
    ch.write("n", getNr());
    ch.write("p", getPoints());
    ch.write("t", getType());
}

void action_bonus::load(class channel &ch) {
    if (ch.key=="n" || ch.key=="nr") { setNr(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="p" || ch.key=="points") { setPoints(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="t" || ch.key=="type") { setType(ch.value); ch.next(); }
}

void action_bonus::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<action_bonus nr=\"" << getNr() <<
    "\" points=\"" << getPoints() <<
    "\" type=\"" << showType() <<
    "\"/>" << endl;
}
